import { Controller, Get, Post, Body, Patch, Param, Delete, HttpException } from '@nestjs/common';
import { TypeService } from './type.service';
import { CreateTypeDto } from './dto/create-type.dto';
import { UpdateTypeDto } from './dto/update-type.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { DeleteTypeDto } from './dto/delete-type.dto';
import { Result } from 'src/utils/result';
import { TypeDocument } from './shema/type.shema';
@ApiTags('标签')
@Controller('type')
export class TypeController {
  constructor(private readonly typeService: TypeService) { }

  @ApiOperation({ summary: '创建标签' })
  @Post('create')
  async create(@Body() createTypeDto: CreateTypeDto):Promise<TypeDocument>{
    // 判断有没有重复的标签
    const isExist = await this.typeService.findByParentId(createTypeDto.parentId);
    console.log(isExist);
    if(isExist.length>0){
      for(let i=0;i<isExist.length;i++){
        if(isExist[i].type===createTypeDto.type){
          throw new HttpException('标签已存在',400);
        }
      }
    }
    return await this.typeService.create(createTypeDto);
  }

  @ApiOperation({ summary: '更新类型' })
  @Post('update')
  async update(@Body() updateTypeDto: UpdateTypeDto):Promise<Boolean>{
    return await this.typeService.update(updateTypeDto);
  }

  @ApiOperation({ summary: '删除类型' })
  @Post('/delete')
  async delete(@Body() deleteTypeDto:DeleteTypeDto):Promise<Boolean>{
    return await this.typeService.deleteOne(deleteTypeDto.id);
  }
  // @ApiOperation({ summary: '获取所有标签' })
  // @Get('list')
  // findList() {
  //   return this.typeService.findAll();
  // }


  // @Post('/delete-all')
  // async delete(@Body() deleteTypeDto:DeleteTypeDto):Promise<any>{
  //   return this.typeService.remove();
  // }
}
